<?php
    $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
    mysql_select_db("mydb", $conn) or die(mysql_error());
    
    include'functions.php';
    include'OfflineTemplate.php';
   
    if(isset($_POST['submit']))
    {
        // The query inserts the comment into the database 
       session_start();
       $commentorID = $_SESSION['member_id'];
       $text = $_POST['comments'];
       $span = '0';
       $paperID = $_GET['paper_id'];
       $verID = get_latest_version($paperID);
       $result = mysql_query("INSERT into comment (commentor_id,span_id,version_id,text) 
                 VALUES ('$commentorID','$span','$verID','$text')")
                 or die("error");
       echo '<script type="text/javascript">',
        'alert("Your comment have been saved successfully");',
        '</script>';
    }
   
    if (isset ($_POST['done']))
    {
        $conf_id = $_GET['conference_id'];
        $value = $_POST['type']; // The value chosen by the reviewer from the radio button
        $id = $_POST['rate']; // the id of the rating criteria chosen from the dropdown list
        //echo $id;
       
        //Selects the rating criteria assigned to the conference by comparing the conference_id of the rate chosen from the dropdownlist
        $query = mysql_query
            ("SELECT Rate_id,Conference_id FROM conference_rating_criteria WHERE conference_rating_criteria.Rate_id ='$id'
             AND conference_rating_criteria.Conference_id ='" . $conf_id . "'") or die('here' . mysql_error());
        $row = mysql_fetch_assoc($query);
        $row['Rate_id'];
        $span = '0';
        $paperID = $_GET['paper_id'];
        $verID = get_latest_version($paperID);
        //echo $verID;
        //The query inserts the rate into the database
        $resultCom = mysql_query("INSERT into version_rating_criteria (version_id,rate_id,span_id,rating_value) VALUES
                ('$verID','$id','$span','$value')");
        
        //The average rate will be calculate by dividing sum of the rating value over sum of rate ids
        $sql_query = mysql_query("select COUNT(rate_id) FROM version_rating_criteria WHERE version_id='{$verID}'") 
        or die(mysql_error());
        $row = mysql_fetch_assoc($sql_query);
        $x = $row['COUNT(rate_id)']; //getting sum of rate ids

        //getting sum of rate values
        $sql_query2 = mysql_query("SELECT SUM(rating_value) FROM version_rating_criteria WHERE version_id='{$verID}'") // sum of all rating values.
                or die(mysql_error());
        $row2 = mysql_fetch_assoc($sql_query2);
        $y = $row2['SUM(rating_value)'];
        $avgRate = $y / $x; // the average rate(dividing sum of rat value over sum of ids)
        
        //A query for making sure that the version_id of the paper is the same I have
        $paper_id = mysql_query("SELECT paper_id FROM version WHERE version_id = '" . $verID . "'")
        or die('here' . mysql_error());
        
        // Inserting the final rate in the tables after making sure of the paper_id and the version of the paper
        $row = mysql_fetch_assoc($paper_id);
        mysql_query("UPDATE paper SET final_rating = '$avgRate' WHERE paper_id = '{$row['paper_id']}'") or die('here1' . mysql_error());
        
        mysql_close($conn);
         echo '<script type="text/javascript">',
        'alert("Your rating have been saved successfully");',
        '</script>';;
    }
     
     
    
?>
